var Mongoose = require('mongoose'),
    util = require('../helpers/util');

var activitySchema = Mongoose.Schema({
    user: {
        type: Mongoose.Schema.ObjectId,
        ref: 'User'
    },
    idea: {
        type: Mongoose.Schema.ObjectId,
        ref: 'Idea'
    },
    comment: {
        type: Mongoose.Schema.ObjectId,
        ref: 'Comment'
    },
    vote: {
        type: Mongoose.Schema.ObjectId,
        ref: 'Vote'
    },
    abuse: {
        type: Mongoose.Schema.ObjectId,
        ref: 'Abuse'
    },
    activityType: {
        type: String,
        default: ''
    }
}, {timestamps: true});

var Activity = Mongoose.model('Activity', activitySchema);

Activity.createNewActivity = function (newActivity, callback) {
    newActivity.save(callback);
};

Activity.getAllActivities = function (userId, params, callback) {
    var skipItem = params.skipItem || 0,
        itemsperPage = params.itemsperPage || 10,
        sortParameter = params.sortParameter || 'createdAt',
        sortOrder = ((params.sortOrder === 'ascending') ? 1 : -1),
        sort = {},
        skip = parseInt(skipItem) || 0,
        limit = parseInt(itemsperPage) || 10;
    sort[sortParameter] = sortOrder;

    searchMatch = {user: Mongoose.Types.ObjectId(userId)};

    Activity.aggregate([

        {$match: searchMatch},
        {"$sort": sort},
        {"$skip": skip},
        {"$limit": limit},
        {
            $project: {
                "_id": "$_id",
                "createdAt": "$createdAt",
                "user": "$user",
                "idea": "$idea",
                "comment": "$comment",
                "abuse": "$abuse",
                "vote": "$vote",
                "activityType": "$activityType"
            }
        },
        {
            $lookup: {
                "from": "ideas",
                "localField": "idea",
                "foreignField": "_id",
                "as": "ideas"
            }
        },
        {
            $unwind: "$ideas"
        },
        {
            $project: {
                "_id": "$_id",
                "createdAt": "$createdAt",
                "user": "$user",
                "idea": "$idea",
                "comment": "$comment",
                "abuse": "$abuse",
                "vote": "$vote",
                "activityType": "$activityType",
                "title": "$ideas.title",
                "content": "$ideas.content",
                "tags": "$ideas.tags"
            }
        },
        {
            $lookup: {
                "from": "users",
                "localField": "user",
                "foreignField": "_id",
                "as": "users"
            }
        },
        {
            $unwind: "$users"
        },
        {
            $project: {
                "_id": "$_id",
                "createdAt": "$createdAt",
                "user": "$user",
                "idea": "$idea",
                "comment": "$comment",
                "abuse": "$abuse",
                "vote": "$vote",
                "activityType": "$activityType",
                "name": {$concat: ["$users.firstName", " ", {$ifNull: ["$users.lastName", " "]}]},
                "title": "$title",
                "content": "$content",
                "tags": "$tags"
            }
        }
    ], callback);
};

Activity.getAllUsersActivitiesCSV = function (params, callback) {
    
    var fromDate = util.getDateWithoutTimeStamp(params.fromDate);
    var todate1 = util.getDateWithoutTimeStamp(params.toDate);
    var endDate = new Date(todate1.getTime() + (24 * 60 * 60 * 1000));

    var searchMatch = {};
    if (params.userId == undefined) {
        searchMatch = {createdAt: {$gte: fromDate, $lt: endDate}};
    } else {
        searchMatch = {$and: [{createdAt: {$gte: fromDate, $lt: endDate}}, {user: Mongoose.Types.ObjectId(params.userId)}]};
    }
    
    // Activity.find(searchMatch, callback);

    Activity.aggregate([
        {$match: searchMatch},
        {
            $lookup: {
                "from": "ideas",
                "localField": "idea",
                "foreignField": "_id",
                "as": "ideas"
            }
        },
        {
            $unwind: "$ideas"
        },
        {
            $project: {
                "_id": "$_id",
                "createdAt": "$createdAt",
                "user": "$user",
                "idea": "$idea",
                "comment": "$comment",
                "abuse": "$abuse",
                "vote": "$vote",
                "activityType": "$activityType",
                "ideaTitle": "$ideas.title",
                "ideaContent": "$ideas.content"
            }
        },
        {
            $lookup: {
                "from": "comments",
                "localField": "comment",
                "foreignField": "_id",
                "as": "comments"
            }
        },
        {
            $project: {
                "_id": "$_id",
                "createdAt": "$createdAt",
                "user": "$user",
                "idea": "$idea",
                "comment": "$comment",
                "abuse": "$abuse",
                "vote": "$vote",
                "activityType": "$activityType",
                "ideaTitle": "$ideaTitle",
                "ideaContent": "$ideaContent",
                "comments": "$comments.comment"
            }
        },
        {
            $lookup: {
                "from": "users",
                "localField": "user",
                "foreignField": "_id",
                "as": "users"
            }
        },
        {
            $unwind: "$users"
        },
        {
            $project: {
                "_id": "$_id",
                "createdAt": "$createdAt",
                "user": "$user",
                "idea": "$idea",
                "comment": "$comment",
                "abuse": "$abuse",
                "vote": "$vote",
                "activityType": "$activityType",
                "ideaTitle": "$ideaTitle",
                "ideaContent": "$ideaContent",
                "comments": "$comments",
                "name": {$concat: ["$users.firstName", " ", {$ifNull: ["$users.lastName", " "]}]}
            }
        }

    ], callback);
};

Activity.getAllActivitiesByDate = function (params, callback) {
    var fromDate = util.getDateWithoutTimeStamp(params.fromDate);
    var todate1 = util.getDateWithoutTimeStamp(params.toDate);
    var endDate = new Date(todate1.getTime() + (24 * 60 * 60 * 1000));

    var searchMatch = {};
    if (params.userId == undefined) {
        searchMatch = {createdAt: {$gte: fromDate, $lt: endDate}};
    } else {
        searchMatch = {$and: [{createdAt: {$gte: fromDate, $lt: endDate}}, {user: Mongoose.Types.ObjectId(params.userId)}]};
    }

    var skipItem = params.skipItem || 0,
        itemsperPage = params.itemsperPage || 20,
        sortParameter = 'createdAt',
        sortOrder = -1,
        sort = {},
        skip = parseInt(skipItem),
        limit = parseInt(itemsperPage);
    sort[sortParameter] = sortOrder;

    Activity.aggregate([
        {$match: searchMatch},
        {"$sort": sort},
        {"$skip": skip},
        {"$limit": limit},
        {
            $lookup: {
                "from": "users",
                "localField": "user",
                "foreignField": "_id",
                "as": "users"
            }
        },
        {
            $unwind: "$users"
        },
        {
            $project: {
                "_id": "$_id",
                "createdAt": "$createdAt",
                "user": "$user",
                "idea": "$idea",
                "comment": "$comment",
                "abuse": "$abuse",
                "vote": "$vote",
                "activityType": "$activityType",
                "name": {$concat: ["$users.firstName", " ", {$ifNull: ["$users.lastName", " "]}]}
            }
        }
    ], callback);
};

Activity.getAllActivitiesByDateCount = function (params, callback) {
    var fromDate = util.getDateWithoutTimeStamp(params.fromDate);
    var todate1 = util.getDateWithoutTimeStamp(params.toDate);
    var endDate = new Date(todate1.getTime() + (24 * 60 * 60 * 1000));

    var searchMatch = {};
    if (params.userId == undefined) {
        searchMatch = {createdAt: {$gte: fromDate, $lt: endDate}};
    } else {
        searchMatch = {$and: [{createdAt: {$gte: fromDate, $lt: endDate}}, {user: Mongoose.Types.ObjectId(params.userId)}]};
    }

    var sortParameter = 'createdAt',
        sortOrder = -1,
        sort = {};
    sort[sortParameter] = sortOrder;

    Activity.aggregate([
        {$match: searchMatch},
        {"$sort": sort},
        {
            $lookup: {
                "from": "users",
                "localField": "user",
                "foreignField": "_id",
                "as": "users"
            }
        },
        {
            $unwind: "$users"
        },
        {
            $project: {
                "_id": "$_id",
                "createdAt": "$createdAt",
                "user": "$user",
                "idea": "$idea",
                "comment": "$comment",
                "abuse": "$abuse",
                "vote": "$vote",
                "activityType": "$activityType",
                "name": {$concat: ["$users.firstName", " ", {$ifNull: ["$users.lastName", " "]}]}
            }
        }
    ], callback);
};

Activity.deleteActivitiesByIdea = function (ideaId, callback) {
    Activity.find(
        {'idea': Mongoose.Types.ObjectId(ideaId)}
    ).remove(callback);
};

module.exports = Activity;
